var express = require('express');
var router = express.Router();
let mongoose = require('mongoose');
// //teacher
// let Teacher = new mongoose.Schema({
//     name: String,
//     courseId:[{
//         type: mongoose.SchemaTypes.ObjectId,
//         ref: 'ScoreModel'  
//     }]
// });
// let Score = new mongoose.Schema({
//     scoreId:Number
// })
// //建立score与模型的关系
// let TeacherModel = mongoose.model('TeacherModel',Teacher, 'teacher');
// let ScoreModel = mongoose.model('ScoreModel',Score, 'score');
// let TeacheraddModel=mongoose.model('TeacheraddModel',Teacher, 'teacher');
// let TeacherdelateModel=mongoose.model('TeacherdelateModel',Teacher, 'teacher');
// let TeacherchangeModel=mongoose.model('TeacherchangeModel',Teacher, 'teacher');

//渲染操作
router.post('/teachershow',async function (req, res) {
   let resTeacher= await mongoose.model('TeacherModel').find({}).populate('courseId');
   res.send(resTeacher);
   console.log(resTeacher);
})
//新增操作
router.post('/teacheradd',async function(req, res) {
    await mongoose.model('TeacheraddModel').create({
        name:req.body.name,      
    })
    res.send({
        mes:'添加成功'
    })
})
//删除
router.post('/teacherdelate',async function (req, res) {
  await mongoose.model('TeacherdelateModel').deleteMany({
    _id:mongoose.Types.ObjectId(req.body._id) 
  }) 
  res.send({
    mes: '删除成功'
}) 
})
//修改
router.post('/teacherchange',async function (req, res){
    await mongoose.model('TeacherchangeModel').updateOne({
        _id:req.body._id
    },{
         name:req.body.name
    })
    res.send({
        mes: '修改成功'
    }) 
})
module.exports = router;